package ui;

import javax.swing.*;
import javax.swing.border.EmptyBorder;

import ui.handlers.CustomerHandler;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.rmi.RemoteException;

public class FindCustomerScreen extends BaseScreen {

	private JFrame mainFrame;
	private JTextField username_tf;
	private JButton submit_btn;
	
	public JButton getSubmitButton(){
		return submit_btn;
	}
	
	public FindCustomerScreen()
	{
		mainFrame = new JFrame();
		mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		mainFrame.setTitle("Find Customer");
		mainFrame.setSize(400, 200);
		mainFrame.setLayout(new BorderLayout());
		
		doTitle();
		doBody();
		doButtons();
		
		mainFrame.setVisible(true);
	}
	
	private void doTitle()
	{
		String title = "<html><font size = \"6\">Enter Customer</font></html>";
		JLabel title_lbl = new JLabel(title);
		mainFrame.add(new JPanel().add(title_lbl), BorderLayout.NORTH);
	}
	
	private void doBody()
	{
		username_tf = new JTextField(15);
		JPanel body_panel = new JPanel();
		body_panel.setLayout(new FlowLayout());
		body_panel.add(new JLabel("Username: "));
		body_panel.add(username_tf);
		body_panel.setBorder(new EmptyBorder(15, 0 ,0 ,0));
		
		mainFrame.add(body_panel, BorderLayout.CENTER);
	}
	
	private void doButtons()
	{
		submit_btn = new JButton("Submit");
		JPanel submit_panel = new JPanel();
		submit_panel.add(submit_btn);
		submit_panel.setBorder(new EmptyBorder(0, 0, 0, 20));
		submit_btn.addActionListener(new CustomerHandler(this));
		
		JPanel button_panel = new JPanel();
		button_panel.setLayout(new GridLayout(1, 3));
		button_panel.add(new JPanel());
		button_panel.add(new JPanel());
		button_panel.add(submit_panel);
		button_panel.setBorder(new EmptyBorder(0, 0, 35, 0));
		
		mainFrame.add(button_panel, BorderLayout.SOUTH);
	}

	public void findCustomer() throws RemoteException
	{
		String customerUsername = username_tf.getText();
		if (store.customerExists(customerUsername))
		{ new PlaceOrderScreen(customerUsername, false); }
		else
		{ new CreateCustomerScreen(); }
	}
	
	public JFrame getMainFrame() {
		return mainFrame;
	}
}